package com.shizhanzhe.gxb.vip.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.shizhanzhe.gxb.R;
import com.shizhanzhe.gxb.url.Path;
import com.shizhanzhe.gxb.utils.Util;
import com.shizhanzhe.gxb.vip.bean.Notice;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by YJ on 2018/10/23.
 */
public class NoticeStudyAdapter extends RecyclerView.Adapter<NoticeStudyAdapter.ViewHolder> {

    private Context mContext;
    private List<Notice.DataBeanX.DataBean> mData = new ArrayList<>();
    private NoticeListener mListener;

    public void setListener(NoticeListener listener) {
        mListener = listener;
    }

    public NoticeStudyAdapter(Context context) {
        mContext = context;
    }

    public void updateData(int page, List<Notice.DataBeanX.DataBean> dataSet) {
        if (dataSet != null && !dataSet.isEmpty()) {
            if (page == 1) {
                mData = dataSet;
            } else {
                mData.addAll(dataSet);
            }
            notifyDataSetChanged();
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.adapter_notice, parent, false);
        return new ViewHolder(view);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        final Notice.DataBeanX.DataBean bean = mData.get(position);
        if (!"".equals(bean.getBanner())){
            if (bean.getBanner().contains("http")){
                Glide.with(mContext).load(bean.getBanner()).into(holder.noticeIv);
            }else {
                Glide.with(mContext).load(Path.BASE_URL + bean.getBanner()).into(holder.noticeIv);
            }
            holder.noticeIv.setVisibility(View.VISIBLE);
        }else {
            holder.noticeIv.setVisibility(View.GONE);
        }
        holder.noticeTitle.setText(bean.getTitle());
//        holder.content.setText("\t\t"+bean.getContent());
        holder.noticeTime.setText(Util.getDateTimeFromMillisecond(bean.getAddtime()));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mListener.itemclisk(bean);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.notice_title)
        TextView noticeTitle;
        @BindView(R.id.notice_time)
        TextView noticeTime;
        @BindView(R.id.notice_iv)
        ImageView noticeIv;


        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
    public interface NoticeListener{
        void itemclisk(Notice.DataBeanX.DataBean bean);
    }
}
